package com.wf.netty.coder.complex;

import com.wf.netty.coder.encoder.Integer2ByteEncoder;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.jupiter.api.Test;

/**
 * 功能描述
 *
 * @author wf
 * @since 2022/5/27
 */
public class MessageToMessageTester {
    @Test
    public void testString2Integer() {
        ChannelInitializer<EmbeddedChannel> channelInitializer = new ChannelInitializer<EmbeddedChannel>() {
            @Override
            protected void initChannel(EmbeddedChannel ch) throws Exception {
                // 出站从尾到头的顺序处理
                ch.pipeline().addLast(new Integer2ByteEncoder());
                ch.pipeline().addLast(new String2IntegerEncoder());
            }
        };
        EmbeddedChannel channel = new EmbeddedChannel(channelInitializer);
        for (int j = 0; j < 100; j++) {
            String s = "i am " + j;
            channel.write(s); //向通道写入含有数字的字符串
        }
        channel.flush();
        ByteBuf buf = channel.readOutbound();
        while (null != buf) {
            System.out.println("o = " + buf.readInt()); //打印数字
            buf = channel.readOutbound();     //读取数字
        }
    }
}
